package com.cssw.fyzb.core.dao.appeal;

import com.cssw.fyzb.core.model.dodb.Appeal;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 */
@Repository
public class AppealDaoImpl implements AppealDao {
    @Override
    public Page<Appeal> listAppeal(Long userId, int pageNumber, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        SqlParse sqlParse = new SqlParse(map);
        sqlParse.addSQL("select t1.*,t2.appeal_type_name type_name from t_appeal t1 left join d_appeal_type t2 on t1.appeal_type=t2.appeal_type  where t1.user_id=:userId ");
        return Appeal.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Appeal getAppeal(Long appealId) {
        StringBuilder str = new StringBuilder();
        str.append("select t1.*,t2.appeal_type_name from t_appeal t1 left join d_appeal_type t2 on t1.appeal_type=t2.appeal_type   where appeal_id=?  ");
        return Appeal.dao.findFirst(str.toString(), appealId);
    }

    @Override
    public Long saveAppeal(Long userId, String appealType, String contact, String appeal, String appealAttach, String status) {
        Appeal a = new Appeal();
        a.setUserId(userId);
        a.setAppealType(appealType);
        a.setContact(contact);
        a.setAppeal(appeal);
        a.setAppealAttach(appealAttach);
        a.setStatus(status);
        a.setAppealTime(new Date());
        a.save();
        return a.getAppealId();
    }

    @Override
    public Page<Appeal> list(int pageNumber, int pageSize, Map param) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select a.*,u.nick_name,t.appeal_type_name as type_name  from t_appeal a ");
        sqlParse.addSQL("left join t_user u on a.user_id=u.user_id ");
        sqlParse.addSQL("left join d_appeal_type t on a.appeal_type = t.appeal_type ");
        sqlParse.addSQL("where 1=1 ");
        sqlParse.addSQL("and a.user_id like :userId ");
        sqlParse.addSQL("and u.nick_name like :nickName ");
        sqlParse.addSQL("and u.mobile like :mobile ");
        sqlParse.addSQL("and a.appeal_time= :appealTime ");
        sqlParse.addSQL(" order by a.appeal_time desc  ");
        return Appeal.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public void save(Long appealId, String feedback, Long perId, String status) {
        Appeal appeal = new Appeal();
        appeal.setAppealId(appealId);
        appeal.setFeedback(feedback);
        appeal.setFeedbackTime(new Date());
        appeal.setStatus(status);
        appeal.setFeedbackOperId(perId);
        appeal.update();
    }

}
